import { Component, OnInit } from '@angular/core'
import { Router, ActivatedRoute } from '@angular/router'
import { NzMessageService } from 'ng-zorro-antd'
import { STColumn } from '@delon/abc'
import { I18NService } from '@core'

import { ListBaseClass } from '@shared/classes/ListBaseClass'
import { User } from '@shared/interfaces/user'
import { IQueryParams } from '@shared/interfaces/queryParams'
import { RoleService } from '../../role.service'
import { DataService } from '@shared/services/data.service'
import { EUserType } from '@shared/enums'
import { StatusTextMap } from '@shared/interfaces/common'

@Component({
  selector: 'app-role-detail-users',
  templateUrl: './users.component.html',
  styleUrls: ['../../../../shared/styles/table.less'],
})
export class RoleDetailUsersComponent extends ListBaseClass<User, IQueryParams> implements OnInit {
  columns: STColumn[] = [
    {
      type: 'link',
      title: '用户名',
      i18n: 'USER.USERNAME',
      index: 'username',
      className: 'text-left pl-md',
      width: '150px',
      fixed: 'left',
    },
    {
      title: '用户姓名',
      i18n: 'USER.NAME',
      index: 'nickName',
      width: '200px',
      className: 'text-left',
    },
    {
      title: '邮箱',
      i18n: 'GENERAL.INFO.EMAIL',
      index: 'email',
      width: '200px',
      className: 'text-left',
    },
    {
      title: '关联层级编号',
      i18n: 'GENERAL.INFO.BIZ_LEVEL_CODE',
      index: 'bizLevelCode',
      width: '100px',
      className: 'text-center',
    },
    {
      title: '状态',
      i18n: 'GENERAL.INFO.STATUS',
      width: '100px',
      className: 'text-center',
      render: 'status',
    },
  ]

  statusTextMap = StatusTextMap

  constructor(
    public msg: NzMessageService,
    public i18n: I18NService,
    public router: Router,
    public route: ActivatedRoute,
    private roleService: RoleService,
    private dataService: DataService,
  ) {
    super(msg, i18n, route, router)
  }

  ngOnInit() {
    this.parseParams().subscribe(() => {
      this.queryTableDataWithParams()
    })
  }

  queryTableDataWithParams(): void {
    this.loading = true
    const { roleId } = this.dataService.role
    this.roleService.getUsersOfOneRole(this.params, roleId).subscribe(response => {
      for (const d of response.data) {
        switch (d.userType) {
          case EUserType.Institution:
            d.bizLevelCode = d.insCode
            break
          case EUserType.Agent:
            d.bizLevelCode = d.agentCode
            break
          case EUserType.Merchant:
            d.bizLevelCode = d.merNum
            break
          case EUserType.Store:
            d.bizLevelCode = d.storeNum
            break
          default:
            break
        }
      }
      this.renderList(response)
    }, this.getListError.bind(this))
  }
}
